package com.starsea.book.ui.newpage

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.constant.EventBus
import com.starsea.book.databinding.FragmentSetUserProfileBinding
import com.starsea.book.lib.dialogs.selector
import com.starsea.book.ui.newpage.utils.PicSelUtils
import com.starsea.book.ui.newpage.utils.loadRoundImage
import com.starsea.book.ui.newpage.viewmodel.UserViewModel
import com.starsea.book.utils.observeEvent
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2023-12-26 09 GMT+8
 * @desc :
 */
class UserProfileFragment() : VMBaseFragment<UserViewModel>(R.layout.fragment_set_user_profile) {

    private val binding by viewBinding(FragmentSetUserProfileBinding::bind)
    override val viewModel by activityViewModels<UserViewModel>()


    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {


        binding.ivHeader.setOnClickListener {
            PicSelUtils.selPic(
                max = 6,
                requireContext(),
                selList = arrayListOf()
            ) { result ->
                showLoading()
                binding.ivHeader.loadRoundImage(requireContext(), result!![0]?.realPath)
                viewModel.uploadImg("", result[0]?.realPath ?: "")
            }
        }
        binding.rllHeader.setOnClickListener {
            PicSelUtils.selPic(
                max = 6,
                requireContext(),
                selList = arrayListOf()
            ) { result ->
                showLoading()
                binding.ivHeader.loadRoundImage(requireContext(), result!![0]?.realPath)
                viewModel.uploadImg("", result[0]?.realPath ?: "")
            }
        }



        binding.rllNickName.setOnClickListener {
            startActivity<EditActivity> {
                putExtra("type", 0)
                putExtra("content", binding.tvNickName.text.toString())
            }
        }

        binding.rllSex.setOnClickListener {
            val actions = arrayListOf(
                "男",
                "女"
            )

            context?.selector(items = actions) { _, i ->
                when (i) {
                    0 -> {
                        binding.tvSex.text = "男"
                        viewModel.updateUserProfile(sex = 1)
                    }

                    1 -> {
                        binding.tvSex.text = "女"
                        viewModel.updateUserProfile(sex = 2)
                    }
                }
            }
        }

        viewModel.getUserProfile()

        viewModel.userProfileResult.observe(this) {
            if (it.status == 200) {
                binding.tvSex.text = if (it.data!!.sex == 1) "男" else "女"
                binding.tvNickName.text = it.data!!.custName ?: ""
                binding.tvAccount.text = it.data!!.account ?: ""
                binding.ivHeader.loadRoundImage(requireContext(), it.data!!.headUrl ?: "")
            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(requireContext())
            } else {
                toastOnUi(it.msg)
            }
        }

        viewModel.uploadImgResult.observe(this) {
            if (it.status == 200) {
                dismissLoading()
                viewModel.updateUserProfile(headerUrl = it.data!!)
            } else if (it.status == 401) {
                dismissLoading()
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(requireContext())
            } else {
                dismissLoading()
                toastOnUi(it.msg)
            }

        }

        viewModel.updateUserInfoSucResult.observe(this) {
            if (it.status == 200) {
                dismissLoading()
                toastOnUi("更新成功")
            } else if (it.status == 401) {
                dismissLoading()
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(requireContext())
            } else {
                dismissLoading()
                toastOnUi(it.msg)
            }
        }
    }


    override fun observeLiveBus() {
        super.observeLiveBus()
        observeEvent<String>(EventBus.EDIT_CONTENT) {
            binding.tvNickName.text = it
            viewModel.updateUserProfile(custName = it)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.setTitle(R.string.title_user_profile)
    }

}